package com.shop.loan.util;

import com.shop.loan.mapper.GoodsByMybatisMapper;
import com.shop.loan.mapper.GoodsSizeByMybatisMapper;
import com.shop.loan.model.entity.GoodsByMybatis;
import com.shop.loan.model.entity.GoodsSizeByMybatis;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Component
public class GoodsUtil {

    @Resource
    GoodsSizeByMybatisMapper goodsSizeByMybatisMapper;

    @Resource
    GoodsByMybatisMapper goodsByMybatisMapper;

    /**
     * 获取商品规格字符串
     *
     * @param goodsSize
     * @return
     */
    public String getGoodsSizeInfo(Integer packageType, GoodsByMybatis goods, GoodsSizeByMybatis goodsSize) {
        StringBuffer info = new StringBuffer();
        info.append("先享后付");
        if (StringUtils.hasLength(goodsSize.getMemory())) {
            info.append(" | " + goodsSize.getMemory());
        }
        return info.toString();
    }

    /**
     * 修改商品库存
     *
     * @param goodsSizeId
     * @return
     */
    public Boolean updateStock(Long goodsSizeId) {
        GoodsSizeByMybatis goodsSizeByMybatis = goodsSizeByMybatisMapper.selectByPrimaryKey(goodsSizeId);

        GoodsSizeByMybatis update = new GoodsSizeByMybatis();
        update.setStock(goodsSizeByMybatis.getStock() - 1);
        update.setRentalStock(goodsSizeByMybatis.getRentalStock() + 1);
        update.setId(goodsSizeId);
        goodsSizeByMybatisMapper.updateByPrimaryKeySelective(update);

        GoodsByMybatis goodsByMybatis = goodsByMybatisMapper.selectByPrimaryKey(goodsSizeByMybatis.getGoodsId());

        GoodsByMybatis updateGoods = new GoodsByMybatis();
        updateGoods.setId(goodsSizeByMybatis.getGoodsId());
        updateGoods.setRentalStock(goodsByMybatis.getRentalStock() + 1);
        updateGoods.setTotalStock(goodsByMybatis.getTotalStock() - 1);
        goodsByMybatisMapper.updateByPrimaryKeySelective(updateGoods);
        return true;
    }


}
